package com.ruoyi.web.controller.wb;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wb.domain.WbPlan;
import com.ruoyi.wb.dto.WbPlanDto;
import com.ruoyi.wb.service.IWbPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 维保计划Controller
 *
 * @author ruoyi
 * @date 2024-10-14
 */
@Api(tags="维保计划信息管理")
@RestController
@RequestMapping("/wb/wbPlan")
public class WbPlanController extends BaseController {
    @Autowired
    private IWbPlanService wbPlanService;

    /**
     * 查询维保计划列表
     */
    @ApiOperation("查询维保计划列表")
    @GetMapping("/list")
    public TableDataInfo list(WbPlan wbPlan) {
        List<WbPlan> list = wbPlanService.selectWbPlanList(wbPlan);
        return getDataTable(list);
    }

    /**
     * 导出维保计划列表
     */
    @ApiOperation("导出维保计划列表")
    @Log(title = "维保计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WbPlan wbPlan) {
        List<WbPlan> list = wbPlanService.selectWbPlanList(wbPlan);
        ExcelUtil<WbPlan> util = new ExcelUtil<WbPlan>(WbPlan.class);
        util.exportExcel(response, list, "维保计划数据");
    }

    /**
     * 获取维保计划详细信息
     */
    @ApiOperation("获取维保计划详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wbPlanService.selectWbPlanById(id));
    }

    /**
     * 新增维保计划
     */
    @ApiOperation("新增维保计划")
    @Log(title = "维保计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WbPlan wbPlan) {
        return toAjax(wbPlanService.insertWbPlan(wbPlan));
    }

    /**
     * 修改维保计划
     */
    @ApiOperation("修改维保计划")
    @Log(title = "维保计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WbPlan wbPlan) {
        return toAjax(wbPlanService.updateWbPlan(wbPlan));
    }

    /**
     * 删除维保计划
     */
    @ApiOperation("删除维保计划")
    @Log(title = "维保计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wbPlanService.deleteWbPlanByIds(ids));
    }

    @ApiOperation("新增维保计划月检/年检")
    @PostMapping("/savePlan")
    public AjaxResult savePlan(@RequestBody WbPlanDto dto) {
        return toAjax(wbPlanService.savePlan(dto));
    }


//
//    @ApiOperation("新增维保计划年检")
//    @PostMapping("/savePlanYear")
//    public AjaxResult savePlanYear(@RequestBody WbPlanDto dto) {
//        return toAjax(wbPlanService.savePlanYear(dto));
//    }
//

    @ApiOperation("修改维保计划")
    @PostMapping("/updatePlan")
    public AjaxResult updatePlan(@RequestBody WbPlanDto dto) {
        return toAjax(wbPlanService.updatePlan(dto));
    }

    @ApiOperation("删除维保计划")
    @PostMapping("/deletePlan")
    public AjaxResult deletePlan(@RequestBody List<Long> ids) {
        return toAjax(wbPlanService.deletePlan(ids));
    }

    @ApiOperation("获取维保计划详情")
    @GetMapping("/getPlanInfoById")
    public AjaxResult getPlanInfo(@RequestParam("planId") Long planId) {
        return success(wbPlanService.getPlanInfo(planId));
    }

    @ApiOperation("获取企业维保计划")
    @GetMapping("/getEnterprisePlan")
    public TableDataInfo getEnterprisePlan(WbPlan wbPlan) {
        startPage();
        List<WbPlan> list = wbPlanService.getEnterprisePlan(wbPlan, getUserId());
        return getDataTable(list);
    }

    @ApiOperation("导出pdf")
    @GetMapping("/exportPdf")
    public ResponseEntity<FileSystemResource> exportPdf(@RequestParam("planId") Long planId) {
        WbPlan wbPlan = wbPlanService.selectWbPlanById(planId);
        if (ObjectUtils.isEmpty(wbPlan)){
          throw new RuntimeException("维保计划为空");
        }
        String pngName = wbPlan.getExaminingReport();
        String str="https://xf.xiaoyiyunwang.com/prod-api/profile/";
        pngName=pngName.replaceAll(str,"D:/ruoyi/uploadPath/");
        if (StringUtils.isNotEmpty(pngName)){
            File file = new File(pngName.substring(0,pngName.length()-4)+".pdf");
            HttpHeaders headers = new HttpHeaders();
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Content-Disposition", "attachment; filename=" + file.getName());
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");
            headers.add("Last-Modified", new Date().toString());
            headers.add("ETag", String.valueOf(System.currentTimeMillis()));
            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .contentLength(file.length())
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(new FileSystemResource(file));
        }else {
            throw new RuntimeException("该计划还未生成pdf");
        }

    }


}